home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
- *
- * @APPLE_LICENSE_HEADER_START@
- *
- * The contents of this file constitute Original Code as defined in and
- * are subject to the Apple Public Source License Version 1.1 (the
- * "License"). You may not use this file except in compliance with the
- * License. Please obtain a copy of the License at
- * http://www.apple.com/publicsource and read it before using this file.
- *
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
- * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
- * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
- * License for the specific language governing rights and limitations
- * under the License.
- *
- * @APPLE_LICENSE_HEADER_END@
- */
- /*
- * MPEnabler - An example of patching funtions in the Mac OS X Kernel
- *
- * Yes, I know that I said you don't patch functions in kernel,
- * but you should remember what your mother used to say,
- *
- * "Do and I say, not as I do."
- *
- * Josh de Cesare, MacHack 2000
- */
-
- #include "PatchIt.h"
-
- extern void bcopy(void *from, void *to, long length);
- extern void bzero(void *addr, long length);
- extern void ml_phys_write(vm_offset_t paddr, unsigned int data);
-
- PatchPtr gPatches;
-
- static vm_offset_t gPatchPage;
- static vm_offset_t gPatchPagePhys;
- static long *gPatchItStubAddr;
-
- void InitPatchIt(void)
- {
- // Get one page of wired memory.
- kmem_alloc_wired(kernel_map, &gPatchPage, PAGE_SIZE);
-
- // Get the physical address of the page.
- gPatchPagePhys = pmap_extract(kernel_pmap, gPatchPage);
-
- // Clear the memory to make sure all the valid's are zero.
- bzero((void *)gPatchPage, PAGE_SIZE);
-
- gPatches = (PatchPtr)gPatchPage;
- gPatchItStubAddr = (long *)PatchItStub;
- }
-
-
- void UnInitPatchIt(void)
- {
- long cnt;
-
- for (cnt = 0; cnt < kMaxPatches; cnt++) {
- UnPatchIt(gPatches + cnt);
- }
-
- kmem_free(kernel_map, gPatchPage, PAGE_SIZE);
- }
-
-
- PatchPtr PatchIt(void *oldFunc, void *newFunc)
- {
- long cnt, physAddr, data;
- unsigned long oldFuncAddr, patchFuncAddr, patchDataAddr;
- PatchPtr patch = 0;
-
- // Find a free slot.
- for (cnt = 0; cnt < kMaxPatches; cnt++) {
- if (!gPatches[cnt].valid) {
- patch = gPatches + cnt;
- break;
- }
- }
- if (patch == 0) return 0;
-
- // Make the patch stub.
- oldFuncAddr = (unsigned long)oldFunc + 0x18;
- patch->patchStub[0] = gPatchItStubAddr[0] | (oldFuncAddr >> 16);
- patch->patchStub[1] = gPatchItStubAddr[1] | (oldFuncAddr & 0xFFFC);
- patch->patchStub[2] = gPatchItStubAddr[2];
- bcopy(oldFunc, patch->patchStub + 3, 0x18);
- patch->patchStub[9] = gPatchItStubAddr[5];
-
- // Flush the stub from data and instruction caches.
- PatchItFlush(gPatchPage + (cnt * kPatchSize));
- PatchItFlush(gPatchPage + (cnt * kPatchSize) + 0x20);
-
- // Get physical addresses for the old function.
- oldFuncAddr = (unsigned long)oldFunc;
- patch->targetPhys1 = pmap_extract(kernel_pmap, (vm_offset_t)oldFunc);
- patch->targetPhys2 = pmap_extract(kernel_pmap, (vm_offset_t)oldFunc + 0x14);
-
- // Install the patch helper.
- patchFuncAddr = (unsigned long)PatchItHelper;
- patchDataAddr = (unsigned long)patch;
-
- physAddr = patch->targetPhys1;
- data = gPatchItStubAddr[0] | (patchFuncAddr >> 16);
- ml_phys_write(physAddr, data);
- physAddr += 4;
-
- if ((physAddr & (PAGE_SIZE - 1)) == 0) physAddr = patch->targetPhys2;
- data = gPatchItStubAddr[1] | (patchFuncAddr & 0xFFFC);
- ml_phys_write(physAddr, data);
- physAddr += 4;
-
- if ((physAddr & (PAGE_SIZE - 1)) == 0) physAddr = patch->targetPhys2;
- data = gPatchItStubAddr[2];
- ml_phys_write(physAddr, data);
- physAddr += 4;
-
- if ((physAddr & (PAGE_SIZE - 1)) == 0) physAddr = patch->targetPhys2;
- data = gPatchItStubAddr[3] | (patchDataAddr >> 16);
- ml_phys_write(physAddr, data);
- physAddr += 4;
-
- if ((physAddr & (PAGE_SIZE - 1)) == 0) physAddr = patch->targetPhys2;
- data = gPatchItStubAddr[4] | (patchDataAddr & 0xFFFF);
- ml_phys_write(physAddr, data);
- physAddr += 4;
-
- if ((physAddr & (PAGE_SIZE - 1)) == 0) physAddr = patch->targetPhys2;
- data = gPatchItStubAddr[5];
- ml_phys_write(physAddr, data);
- physAddr += 4;
-
- // Flush the patch from data and instruction caches.
- PatchItFlush((vm_offset_t)oldFunc);
- PatchItFlush((vm_offset_t)oldFunc + 0x14);
-
- patch->oldFunc = oldFunc;
- patch->newFunc = newFunc;
- patch->valid = 1;
-
- return patch;
- }
-
- void UnPatchIt(PatchPtr patch)
- {
- unsigned long oldFuncAddr, physAddr, data;
-
- if (!patch->valid) return;
- patch->valid = 0;
-
- // Uninstall the patch.
- physAddr = patch->targetPhys1;
- data = patch->patchStub[3];
- ml_phys_write(physAddr, data);
- physAddr += 4;
-
- if ((physAddr & (PAGE_SIZE - 1)) == 0) physAddr = patch->targetPhys2;
- data = patch->patchStub[4];
- ml_phys_write(physAddr, data);
- physAddr += 4;
-
- if ((physAddr & (PAGE_SIZE - 1)) == 0) physAddr = patch->targetPhys2;
- data = patch->patchStub[5];
- ml_phys_write(physAddr, data);
- physAddr += 4;
-
- if ((physAddr & (PAGE_SIZE - 1)) == 0) physAddr = patch->targetPhys2;
- data = patch->patchStub[6];
- ml_phys_write(physAddr, data);
- physAddr += 4;
-
- if ((physAddr & (PAGE_SIZE - 1)) == 0) physAddr = patch->targetPhys2;
- data = patch->patchStub[7];
- ml_phys_write(physAddr, data);
- physAddr += 4;
-
- if ((physAddr & (PAGE_SIZE - 1)) == 0) physAddr = patch->targetPhys2;
- data = patch->patchStub[8];
- ml_phys_write(physAddr, data);
- physAddr += 4;
-
- // Flush the original from data and instruction caches.
- oldFuncAddr = (unsigned int)patch->oldFunc;
- PatchItFlush(oldFuncAddr);
- PatchItFlush(oldFuncAddr + 0x14);
-
- patch->valid = 0;
- }
-